import { copyFileSync, existsSync, mkdirSync, readdirSync, rmSync } from 'fs'
import path from 'path'

const dist = path.join(__dirname, './dist')
const imageDist = path.join(__dirname, './dist/image')
const cssDist = path.join(__dirname, './dist/css')

if (existsSync(dist)) rmSync(dist, { recursive: true })

mkdirSync(dist)
mkdirSync(imageDist)
mkdirSync(cssDist)

function copyFiles() {
  const popupHtml = path.join(__dirname, './src/popup.html')
  const popupHtmlDist = path.join(__dirname, './dist/popup.html')
  copyFileSync(popupHtml, popupHtmlDist)
  const manifest = path.join(__dirname, './src/manifest.json')
  const manifestDist = path.join(__dirname, './dist/manifest.json')
  copyFileSync(manifest, manifestDist)

  const lib = path.join(__dirname, './src/lib')
  const image = path.join(__dirname, './src/image')
  const css = path.join(__dirname, './src/css')
  readdirSync(lib).map(l => copyFileSync(`${lib}/${l}`, `${dist}/${l}`))
  readdirSync(image).map(l => copyFileSync(`${image}/${l}`, `${imageDist}/${l}`))
  readdirSync(css).map(l => copyFileSync(`${css}/${l}`, `${cssDist}/${l}`))
}
copyFiles()
